package com.xiaoqi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoqi.constant.MessageConstant;
import com.xiaoqi.dao.SetMealDao;
import com.xiaoqi.dao.SetmealMobileDao;
import com.xiaoqi.exception.SunnydayException;
import com.xiaoqi.pojo.PageResult;
import com.xiaoqi.pojo.QueryPageBean;
import com.xiaoqi.pojo.Setmeal;
import com.xiaoqi.service.SetMealService;
import com.xiaoqi.utils.QiNiuUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import java.io.BufferedWriter;
import java.io.FileOutputStream;

import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PC
 * creationTime: 2022/10/20 14:04
 */
@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceimpl implements SetMealService {

    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private SetmealMobileDao setmealMobileDao;

    @Autowired
    private FreeMarkerConfig freeMarkerConfig;

    private String out_put_path ="D:\\sunnyday-poject\\sunnyday_parent\\sunnyday_modile\\src\\main\\webapp\\pages\\";

    /**
     * 根据条件分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        //开启分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //跟着分页插件后的第一条查询语句会被分页
        List<Setmeal> setmealList = setMealDao.findAll(queryPageBean.getQueryString());
        //获取更多分页信息
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());

    }

    /**
     * 根据套餐'id'删除套餐
     * @param id
     */
    @Override
    public void del(Integer id) {
        //先查套餐是否以在订单中
        Integer count = setMealDao.findOrderCountById(id);
        if (count>0){
            throw new SunnydayException(MessageConstant.SETMEAL_IN_ORDER);
        }
        //删除此套餐与检查项的关联
        setMealDao.delSetMeal_CheckGroup(id);
        //最后删除套餐
        setMealDao.del(id);
        //静态化
        generateMobileStaticHtml();

    }

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void addSetMeal(Setmeal setmeal,Integer [] checkgroupIds) {
        //增加套餐
        setMealDao.addSetMeal(setmeal);
        //获取增加后的id
        Integer id = setmeal.getId();
        //将所选择的检查组与套餐关联
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.addSetMealCheckGroup(id,checkgroupId);
        }
        //静态化
        generateMobileStaticHtml();

    }

    /**
     * 通过id查询套餐
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        return setMealDao.findById(id);
    }

    /**
     * 查询套餐关联的所有检查组的id
     * @param id
     * @return
     */
    @Override
    public List<Integer> findSetMeal_CheckGroupByid(Integer id) {
        return setMealDao.findSetMeal_CheckGroupByid(id);
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        //先删除套餐之前所关联的所有检查组
        setMealDao.delSetMeal_CheckGroup(setmeal.getId());
        //添加新的套餐与检查组之间的关系
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.addSetMealCheckGroup(setmeal.getId(),checkgroupId);
        }
        //更改菜单
        setMealDao.update(setmeal);
        //静态化
        generateMobileStaticHtml();
    }
    /**
     * 查询数据库所有菜单的图片
     * @return
     */
    @Override
    public List<String> findImgs() {
        return setMealDao.findImgs();
    }


    /**
     * 生成移动端静态html
     */
    private void generateMobileStaticHtml(){
        try {
            generateSetMealHtml();
            generateSetMealDetailHtml();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成静态化页面
     * @param ftlpath
     * @param data
     * @param out_put_path
     */
    private void generateHtml(String ftlpath, Map<String,Object> data,String out_put_path ) throws Exception {
        //获取freemarker配置类
        Configuration configuration = freeMarkerConfig.getConfiguration();
        //写入流
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out_put_path),"utf-8"));
        //获取模板
        Template template = configuration.getTemplate(ftlpath);
        //生成静态页面
        template.process(data,bufferedWriter);
        //刷新缓冲流
        bufferedWriter.flush();
        //关流
        bufferedWriter.close();
    }

    /**
     * 生成静态化后的setmeal页面
     */
    private void generateSetMealHtml() throws Exception {
        //生成数据
        List<Setmeal> setmealList = setmealMobileDao.findAll();
        //将图片拼接外域名，组成一个完整的查看图片地址
        for (Setmeal setmeal : setmealList) {
            System.out.println(setmeal.getRemark());
            setmeal.setImg(QiNiuUtils.DOMAIN+setmeal.getImg());
        }
        //生产方法参数map
        HashMap<String, Object> data = new HashMap<>();
        //加入到map中
        data.put("setmealList",setmealList);
        //调用生成的方法
        generateHtml("mobile_setmeal.ftl",data,out_put_path+"mobile_setmeal.html");
    }
    /**
     * 生成静态化后的setmeal页面
     */
    private void generateSetMealDetailHtml() throws Exception {
        //生成数据
        List<Setmeal> setmealList = setmealMobileDao.findAll();
        //将图片拼接外域名，组成一个完整的查看图片地址
        for (Setmeal setmeal : setmealList) {
            //查询套餐详情
            Setmeal SetmealDetail = setmealMobileDao.findDetailById(setmeal.getId());
            //拼接图片
            SetmealDetail.setImg(QiNiuUtils.DOMAIN+SetmealDetail.getImg());
            //生产方法参数map
            HashMap<String, Object> data = new HashMap<>();
            //加入到map中
            data.put("setmeal",SetmealDetail);
            //调用生成的方法
            generateHtml("mobile_setmeal_detail.ftl",data,out_put_path+"mobile_setmeal_"+SetmealDetail.getId()+".html");
        }

    }
}
